﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace mediaControl.ViewModels
{
    public class ScreenSettingViewModel : BindableBase, IDialogAware
    {
        public string Title { get; set; }
        public string ScreenName { get { return screenName; } set { this.screenName = value; RaisePropertyChanged(); } }
        public int Width { get { return width; } set { this.width = value; RaisePropertyChanged(); } }
        public int Height { get { return height; } set { this.height = value; RaisePropertyChanged(); } }
        public int OrignX { get { return orignX; } set { this.orignX = value; RaisePropertyChanged(); } }
        public int OrignY { get { return orignY; } set { this.orignY = value; RaisePropertyChanged(); } }
        public decimal RotateAngle { get { return rotateAngle; } set { this.rotateAngle = value; RaisePropertyChanged(); } }

        private string screenName;

        private int width;

        private int height;

        private int orignX;

        private int orignY;

        private decimal rotateAngle;

        private ScreenVo oldScreen;

        public event Action<IDialogResult> RequestClose;

        //事件聚合器
        private readonly IEventAggregator eventAggregator;

        private ObservableCollection<ScreenVo> screenList = new ObservableCollection<ScreenVo>();


        public DelegateCommand ConfirmCommand { set; get; }

        public ScreenSettingViewModel(IEventAggregator eventAggregator)
        {
            ConfirmCommand = new DelegateCommand(newScreen);
            this.eventAggregator = eventAggregator;
        }
        int j = 0;
        private void newScreen()
        {
            j = 0;
            //保存屏幕信息
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "/Config/screenlist.info";
            try
            {
                IDialogParameters parameters = new DialogParameters();
                if (File.Exists(filePath))
                {
                    string content = WriteFileUtil.readFile(filePath);
                    if (!String.IsNullOrEmpty(content))
                    {
                        screenList = JsonConvert.DeserializeObject<ObservableCollection<ScreenVo>>(content);
                        j=screenList.Count;
                    }
                }
                if (null == oldScreen)
                {
                    long nextId = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    ScreenVo screenVo = new ScreenVo(ScreenName, Width, Height, OrignX, OrignY, RotateAngle, nextId);
                    parameters.Add("newScreen", screenVo);
                    screenList.Add(screenVo);
                }
                else
                {   //修改屏幕设置
                    foreach (ScreenVo screenVo2 in screenList)
                    {
                        if (screenVo2.Id == oldScreen.Id) {
                            screenVo2.ScreenName = ScreenName;
                            screenVo2.Width = Width;
                            screenVo2.OrignX = OrignX;
                            screenVo2.Height = Height;
                            screenVo2.OrignY = OrignY;
                            screenVo2.RotateAngle = RotateAngle;

                            oldScreen.ScreenName = ScreenName;
                            oldScreen.Width = Width;
                            oldScreen.OrignX = OrignX;
                            oldScreen.Height = Height;
                            oldScreen.OrignY = OrignY;
                            oldScreen.RotateAngle = RotateAngle;
                            oldScreen.RegionName = screenVo2.RegionName;
                            eventAggregator.GetEvent<ScreenChangeEvent>().Publish(screenVo2);
                            break;
                        }
                    }
                   
                }
                
                //保存节目信息
                string outputJSON = JsonConvert.SerializeObject(screenList);
                
                WriteFileUtil.writeFile(filePath, outputJSON, "2");
                //通知列表显示
                
                parameters.Add("oldScreen", oldScreen);
                eventAggregator.GetEvent<ScreenListEvent>().Publish(screenList);
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"无法创建文件：{ex.Message}");
                MessageBox.Show($"无法创建文件：{ex.Message}");
                return;
            }
            
            
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = "设置屏幕";
           
            //flag="1" 编辑
           oldScreen = parameters.GetValue<ScreenVo>("screen");
           if (oldScreen != null) {
                ScreenName = oldScreen.ScreenName;
                Width = oldScreen.Width;
                Height = oldScreen.Height;
                OrignX = oldScreen.OrignX;
                OrignY = oldScreen.OrignY;
                RotateAngle = oldScreen.RotateAngle;
           }
        }
    }
}
